package com.laxcen.controller2;

import com.github.pagehelper.PageInfo;
import com.laxcen.smoke.config.MyLog;
import com.laxcen.smoke.dto.req.*;
import com.laxcen.smoke.dto.res.ResourceListRes;
import com.laxcen.smoke.service.ResourceService;
import com.laxcen.smoke.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "资源管理")
@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @ApiOperation(value = "添加资源",notes =
            "请求信息： \n" +
                    "&emsp; name &nbsp; : &nbsp; 资源名 \n" +
                    "&emsp; type &nbsp; : &nbsp; (菜单menu 按钮button) \n" +
                    "&emsp; url &nbsp; : &nbsp; 资源地址  \n" +
                    "&emsp; permission &nbsp; : &nbsp; 资源权限(前后端约定好) \n" +
                    "&emsp; parent_id &nbsp; : &nbsp; 父级资源 \n" +
                    "&emsp; sort &nbsp; : &nbsp; 排序 \n" +
                    "&emsp; external &nbsp; : &nbsp; 外部资源\n" +
                    "&emsp; available &nbsp; : &nbsp; 可用\n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名\n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/insert")
    @MyLog(operation = "添加资源")
    @RequiresPermissions("resource:add")
    public Response insert(@RequestBody AddResourceReq addResourceReq ){
        return resourceService.insert(addResourceReq.getName(),addResourceReq.getType(),addResourceReq.getUrl(),addResourceReq.getPermission(),addResourceReq.getParent_id(),addResourceReq.getSort(),addResourceReq.getExternal(),addResourceReq.getAvailable());
    }

    @ApiOperation(value = "修改资源",notes =
            "请求信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 资源id \n" +
                    "&emsp; name &nbsp; : &nbsp; 资源名 \n" +
                    "&emsp; type &nbsp; : &nbsp; 资源类型(菜单menu 按钮button) \n" +
                    "&emsp; url &nbsp; : &nbsp; 资源地址  \n" +
                    "&emsp; permission &nbsp; : &nbsp; 资源权限(前后端约定好) \n" +
                    "&emsp; parent_id &nbsp; : &nbsp; 父级资源 \n" +
                    "&emsp; sort &nbsp; : &nbsp; 排序 \n" +
                    "&emsp; external &nbsp; : &nbsp; 外部资源\n" +
                    "&emsp; available &nbsp; : &nbsp; 可用\n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名\n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/update")
    @MyLog(operation = "修改资源")
    @RequiresPermissions("resource:edit")
    public Response update(@RequestBody UpdateResourceReq updateResourceReq ){
        return resourceService.update(updateResourceReq.getId(),updateResourceReq.getName(),updateResourceReq.getType(),updateResourceReq.getUrl(),updateResourceReq.getPermission(),updateResourceReq.getParent_id(),updateResourceReq.getSort(),updateResourceReq.getExternal(),updateResourceReq.getAvailable());
    }

    @ApiOperation(value = "删除资源",notes =
            "请求信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 资源id \n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名\n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/delete")
    @MyLog(operation = "删除资源")
    @RequiresPermissions("resource:delete")
    public Response delete(@RequestBody CommonOneLongIdReq commonOneLongIdReq ){
        return resourceService.delete(commonOneLongIdReq.getId());
    }

    @ApiOperation(value = "批量删除资源",notes =
            "请求信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; 资源id(多个用英文,隔开) \n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名\n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/deletemore")
    @MyLog(operation = "批量删除资源")
    @RequiresPermissions("resource:batchDelete")
    public Response deletemore(@RequestBody CommonOneIdReq commonOneIdReq ){
        return resourceService.deletemore(commonOneIdReq.getId());
    }

    @ApiOperation(value = "获取资源列表",notes =
            "请求信息： \n" +
                    "&emsp; pageNo &nbsp; : &nbsp; 当前页数 \n" +
                    "&emsp; pageSize &nbsp; : &nbsp; 每页的数据条数 \n" +
            "返回信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; id \n" +
                    "&emsp; name &nbsp; : &nbsp; 资源名 \n" +
                    "&emsp; type &nbsp; : &nbsp; 资源类型(菜单menu 按钮button) \n" +
                    "&emsp; url &nbsp; : &nbsp; 资源地址  \n" +
                    "&emsp; permission &nbsp; : &nbsp; 资源权限(前后端约定好) \n" +
                    "&emsp; parent_id &nbsp; : &nbsp; 父级资源 \n" +
                    "&emsp; sort &nbsp; : &nbsp; 排序 \n" +
                    "&emsp; external &nbsp; : &nbsp; 外部资源\n" +
                    "&emsp; available &nbsp; : &nbsp; 可用(1可用 2不可用)\n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名\n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/get")
    @RequiresPermissions("resource")
    public Response<PageInfo<ResourceListRes>> getAllList(@RequestBody PageInfoReq pageInfoReq ){

        if (pageInfoReq.getPageNo()==null){
            pageInfoReq.setPageNo(1);
        }
        if (pageInfoReq.getPageSize()==null){
            pageInfoReq.setPageSize(10);
        }
        if (pageInfoReq.getPageNo()==null&&pageInfoReq.getPageSize()==null){
            pageInfoReq.setPageNo(1);
            pageInfoReq.setPageSize(10);
        }

        return resourceService.get(pageInfoReq.getPageNo(),pageInfoReq.getPageSize());
    }


    @ApiOperation(value = "导航栏搜索资源列表",notes =
            "请求信息： \n" +
                    "&emsp; name &nbsp; : &nbsp; 资源名 \n" +
                    "&emsp; pageNo &nbsp; : &nbsp; 当前页数 \n" +
                    "&emsp; pageSize &nbsp; : &nbsp; 每页的数据条数 \n" +
                    "返回信息： \n" +
                    "&emsp; id &nbsp; : &nbsp; id \n" +
                    "&emsp; name &nbsp; : &nbsp; 资源名 \n" +
                    "&emsp; type &nbsp; : &nbsp; 资源类型(菜单menu 按钮button) \n" +
                    "&emsp; url &nbsp; : &nbsp; 资源地址  \n" +
                    "&emsp; permission &nbsp; : &nbsp; 资源权限(前后端约定好) \n" +
                    "&emsp; parent_id &nbsp; : &nbsp; 父级资源 \n" +
                    "&emsp; sort &nbsp; : &nbsp; 排序 \n" +
                    "&emsp; external &nbsp; : &nbsp; 外部资源\n" +
                    "&emsp; available &nbsp; : &nbsp; 可用(1可用 2不可用)\n" +
                    "&emsp; username &nbsp; : &nbsp; 用户名\n" +
                    "返回码： \n" +
                    "&emsp; 200 &nbsp; : &nbsp; 成功  \n" +
                    "&emsp; 500 &nbsp; : &nbsp; 服务器错误  ")
    @PostMapping(value = "/gettab")
    @RequiresPermissions("resource:get")
    public Response<PageInfo<ResourceListRes>> getAllListByTab(@RequestBody ResourceListByTab pageInfoReq ){

        if (pageInfoReq.getPageNo()==null){
            pageInfoReq.setPageNo(1);
        }
        if (pageInfoReq.getPageSize()==null){
            pageInfoReq.setPageSize(10);
        }
        if (pageInfoReq.getPageNo()==null&&pageInfoReq.getPageSize()==null){
            pageInfoReq.setPageNo(1);
            pageInfoReq.setPageSize(10);
        }

        return resourceService.getByTab(pageInfoReq.getName(),pageInfoReq.getPageNo(),pageInfoReq.getPageSize());
    }


}
